﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "usemysql.h"
#include <QVBoxLayout>
#include "newwindow.h"
#include <QTableWidgetItem>
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //UseMySQL::instance();
    initWindow();
}

int MainWindow::ID=1;

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initWindow()
{
    setWindowTitle("工程管理器");
    setTable();
}

void MainWindow::setTable()
{
    QStringList headers{"工程ID","工程名称","路径","分辨率","版本","修改日期","描述"};
    ui->tableWidget->setColumnCount(7);
    ui->tableWidget->setHorizontalHeaderLabels(headers);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    QHeaderView *horizontalHeader = ui->tableWidget->horizontalHeader();

    // 设置第0列宽度为100像素，并禁止用户调整
    horizontalHeader->setSectionResizeMode(2, QHeaderView::Fixed);
    ui->tableWidget->setColumnWidth(2, 700);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    QApplication::quit();

}

//void MainWindow::initWindow()
//{
//    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);

//    QWidget *mainContainer = new QWidget(this);
//    QVBoxLayout *mainLayout = new QVBoxLayout(mainContainer);
//    mainLayout->setSpacing(0);
//    mainLayout->setContentsMargins(0, 0, 0, 0);

//    // 1.添加自定义标题栏
//    TitleBar *customTitle = new TitleBar(this);
//    customTitle->setStyleSheet("background-color: rgba(255, 0, 0, 100)");
//    customTitle->setTitleIcon(":/title.ico");
//    customTitle->setTitleContent("工程管理器");
//    mainLayout->addWidget(customTitle);

//    // 2.添加原生菜单栏（需从QMainWindow提取）
//    QMenuBar *menuBar = this->menuBar();
//    menuBar->setParent(mainContainer); // 解除QMainWindow绑定
//    menuBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
//    mainLayout->addWidget(menuBar);

//    QToolBar *toolBar=ui->toolBar;
//    toolBar->setParent(mainContainer);
//    mainLayout->addWidget(toolBar);

//    // 3.添加中心内容区域
//    QWidget *contentWidget = new QWidget();
//    mainLayout->addWidget(contentWidget);

//    // 设置为主窗口中心部件
//    setCentralWidget(mainContainer);


//}


//void MainWindow::initTitleBar(const QString &filePath, const QString &titleContent)
//{
//    titleBar=new TitleBar(this);
//    titleBar->setTitleIcon(filePath);
//    titleBar->setTitleContent(titleContent);
//    //titleBar->move(0,0);

//}


void MainWindow::on_action_new_triggered()
{
    newWindow *newwindow=new newWindow();
    if(newwindow)
    {
        newwindow->show();
    }
    else
    {
        return;
    }

    connect(newwindow,&newWindow::dataReady,this,&MainWindow::on_dataReady);

}

void MainWindow::on_dataReady(QString name, QString path, QString width, QString height, QString info)
{
    QStringList data={QString::number(ID++),name,path,
                      QString(width+"*"+height),"23.1.000",QDateTime::currentDateTime().toString(),info};
    int row=ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(row);
    for(int col=0;col<data.size();col++)
    {
        ui->tableWidget->setItem(row,col,new QTableWidgetItem(data[col]));
    }
}
